package org.jeecg.modules.flowermater.service.impl;

import org.jeecg.modules.flowermater.entity.FlowerMaterialOutMain;
import org.jeecg.modules.flowermater.entity.FlowerMaterialOut;
import org.jeecg.modules.flowermater.mapper.FlowerMaterialOutMapper;
import org.jeecg.modules.flowermater.mapper.FlowerMaterialOutMainMapper;
import org.jeecg.modules.flowermater.service.IFlowerMaterialBookService;
import org.jeecg.modules.flowermater.service.IFlowerMaterialOutMainService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 采后物资出库主表
 * @Author: jeecg-boot
 * @Date:   2022-07-13
 * @Version: V1.0
 */
@Service
public class FlowerMaterialOutMainServiceImpl extends ServiceImpl<FlowerMaterialOutMainMapper, FlowerMaterialOutMain> implements IFlowerMaterialOutMainService {

	@Autowired
	private FlowerMaterialOutMainMapper flowerMaterialOutMainMapper;
	@Autowired
	private FlowerMaterialOutMapper flowerMaterialOutMapper;


	@Override
	@Transactional
	public void saveMain(FlowerMaterialOutMain flowerMaterialOutMain, List<FlowerMaterialOut> flowerMaterialOutList) {
		flowerMaterialOutMainMapper.insert(flowerMaterialOutMain);
		if(flowerMaterialOutList!=null && flowerMaterialOutList.size()>0) {
			for(FlowerMaterialOut entity:flowerMaterialOutList) {
				//外键设置
				entity.setMainid(flowerMaterialOutMain.getId());
				flowerMaterialOutMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(FlowerMaterialOutMain flowerMaterialOutMain,List<FlowerMaterialOut> flowerMaterialOutList) {
		if(flowerMaterialOutMain.getState()==2){
			throw new RuntimeException("库管已经审核，不能修改");
		}
		if(flowerMaterialOutMain.getState()==1){
			throw new RuntimeException("领导已经审核，不能修改");
		}
		if(flowerMaterialOutMain.getState()!=0){
			throw new RuntimeException("不能修改");
		}
		flowerMaterialOutMainMapper.updateById(flowerMaterialOutMain);
		//1.先删除子表数据
		flowerMaterialOutMapper.deleteByMainId(flowerMaterialOutMain.getId());
		//2.子表数据重新插入
		if(flowerMaterialOutList!=null && flowerMaterialOutList.size()>0) {
			for(FlowerMaterialOut entity:flowerMaterialOutList) {
				//外键设置
				entity.setMainid(flowerMaterialOutMain.getId());
				flowerMaterialOutMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		FlowerMaterialOutMain materialOutMain = this.getById(id);
		if(materialOutMain.getState()==2){
			throw new RuntimeException("库管已经审核，不能删除");
		}
		if(materialOutMain.getState()==1){
			throw new RuntimeException("领导已经审核，不能删除");
		}
		if(materialOutMain.getState()!=0){
			throw new RuntimeException("不能删除");
		}
		flowerMaterialOutMapper.deleteByMainId(id);
		flowerMaterialOutMainMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			FlowerMaterialOutMain materialOutMain = this.getById(id);
			if(materialOutMain.getState()==2){
				throw new RuntimeException("库管已经审核，不能删除");
			}
			if(materialOutMain.getState()==1){
				throw new RuntimeException("领导已经审核，不能删除");
			}
			if(materialOutMain.getState()!=0){
				throw new RuntimeException("不能删除");
			}
			flowerMaterialOutMapper.deleteByMainId(id.toString());
			flowerMaterialOutMainMapper.deleteById(id);
		}
	}
	
}
